package org.idea.qiyu.threadpool.demo.exception;

import java.util.concurrent.*;

public class ThreadPoolEatException {

    public static void main(String[] args) {
		ThreadFactory threadFactory = new ThreadFactory() {
			@Override
			public Thread newThread(Runnable r) {
				Thread t = new Thread(r);
				if (t.isDaemon())
					t.setDaemon(false);
				if (t.getPriority() != Thread.NORM_PRIORITY)
					t.setPriority(Thread.NORM_PRIORITY);
				t.setUncaughtExceptionHandler((t1,e) ->{
					System.err.println(t1 + " throws exception: "+ e);
				});
				return t;
			}
		};

        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1,
				1,
				0,
				TimeUnit.SECONDS,
                new SynchronousQueue<Runnable>(),
				threadFactory,
				new ThreadPoolExecutor.DiscardPolicy());

        ThreadPoolExecutor testThreadPoolExecutor = new ThreadPoolExecutor(1,
				2,
				0,
				TimeUnit.SECONDS,
				new ArrayBlockingQueue<>(10));

        for (int i = 0; i < 3; i++) {
				// 无法打印堆栈信息
				testThreadPoolExecutor.execute(new DivTask(99, i));
        }
    }


}
